<?php

namespace App\Http\Controllers\Admin\V1;

use Throwable;
use Illuminate\Http\JsonResponse;
use App\Services\Admin\V1\RoleService;
use App\Extensions\Log\V1\InstanceLog;
use App\Exceptions\Admin\V1\AdminException;
use App\Exceptions\Admin\V1\GeneralException;
use App\Http\Resources\Admin\V1\RoleResource;
use App\Http\Resources\Admin\V1\RoleCollection;
use App\Http\Requests\Admin\V1\RoleStoreRequest;
use App\Http\Requests\Admin\V1\RoleIndexRequest;
use App\Http\Requests\Admin\V1\RoleUpdateRequest;
use App\Http\Requests\Admin\V1\RoleDestroyRequest;
use Illuminate\Database\Eloquent\ModelNotFoundException;

// 控制器编号03
class RoleController extends Controller
{
    /**
     * @var RoleService
     */
    private $role_service;

    /**
     * RoleController constructor.
     * @param RoleService $roleService
     */
    public function __construct(RoleService $roleService)
    {
        $this->role_service = $roleService;
    }

    /**
     * @expalin 获取全部菜单
     * @return JsonResponse
     * @method
     * @author 陈文小超
     * @maxim from专注to执行
     * @time 2021/3/31/11:21
     */
    public function all()
    {
        $roles = $this->role_service->all();

        return $this->success(RoleResource::collection($roles));
    }

    /**
     * @expalin 获取角色列表
     * @param RoleIndexRequest $roleIndexRequest
     * @return JsonResponse
     * @method
     * @author 陈文小超
     * @maxim from专注to执行
     * @time 2021/3/31/11:49
     */
    public function index(RoleIndexRequest $roleIndexRequest)
    {
        $size = $roleIndexRequest->input('size') ?? config('common.page_size');

        $role_info = $this->role_service->paginate($size);

        return $this->success(new RoleCollection($role_info));
    }

    /**
     * @expalin 新增角色
     * @param RoleStoreRequest $roleStoreRequest
     * @return JsonResponse
     * @throws AdminException
     * @method
     * @author 陈文小超
     * @maxim from专注to执行
     * @time 2021/3/31/14:47
     */
    public function store(RoleStoreRequest $roleStoreRequest)
    {
        try {
            $this->role_service->store($roleStoreRequest);
        }
        catch ( Throwable $throwable ) {
            InstanceLog::instance('admin')->error('角色创建失败:' . $throwable->getMessage());

            throw new AdminException(AdminException::ROLE_ROLE_CREATE_FAULT);
        }

        $result = [
            'status'  => trans('admin/v1/controller.general.success_status'),
            'message' => trans('admin/v1/controller.role.success_role_store'),
        ];

        return $this->success($result);
    }

    /**
     * @expalin 更新角色
     * @param RoleUpdateRequest $roleUpdateRequest
     * @return JsonResponse
     * @throws AdminException
     * @throws GeneralException
     * @method
     * @author 陈文小超
     * @maxim from专注to执行
     * @time 2021/3/31/15:49
     */
    public function update(RoleUpdateRequest $roleUpdateRequest)
    {
        try {
            $role = $this->role_service->find($roleUpdateRequest->input('id'));
        }
        catch ( ModelNotFoundException $exception ) {
            InstanceLog::instance('admin')->error('更新角色失败:' . $exception->getMessage());

            throw new GeneralException(GeneralException::GENERAL_DATA_FIND__WITHOUT);
        }

        if ($role->isSuperAdmin()) {
            throw new AdminException(AdminException::ADMIN_ADMIN_DELETE_INVALID);
        }

        try {
            $this->role_service->update($role, $roleUpdateRequest);
        }
        catch ( ModelNotFoundException $exception ) {
            throw new GeneralException(GeneralException::GENERAL_DATA_FIND__WITHOUT);
        }
        catch ( \Throwable $throwable ) {
            InstanceLog::instance('admin')->error('更新修改失败:' . $throwable->getMessage());

            throw new AdminException(AdminException::ROLE_ROLE_UPDATE_FAULT);
        }

        $result = [
            'status'  => trans('admin/v1/controller.general.success_status'),
            'message' => trans('admin/v1/controller.role.success_role_update'),
        ];

        return $this->success($result);
    }

    /**
     * @expalin 删除角色
     * @param RoleDestroyRequest $roleDestroyRequest
     * @return JsonResponse
     * @throws AdminException
     * @throws GeneralException
     * @method
     * @author 陈文小超
     * @maxim from专注to执行
     * @time 2021/3/31/16:01
     */
    public function destroy(RoleDestroyRequest $roleDestroyRequest)
    {
        try {
            $role = $this->role_service->find($roleDestroyRequest->input('id'));
        }
        catch ( ModelNotFoundException $exception ) {
            throw new GeneralException(GeneralException::GENERAL_DATA_FIND__WITHOUT);
        }

        if ($role->isSuperAdmin()) {
            throw new AdminException(AdminException::ADMIN_ADMIN_DELETE_INVALID);
        }

        try {
            $result = $this->role_service->destroy($role);
        }
        catch ( \Exception $exception ) {
            InstanceLog::instance('admin')->error('删除角色失败:' . $exception->getMessage());

            throw new AdminException(AdminException::ROLE_ROLE_DELETE_FAULT);
        }

        if (!$result) {
            throw new AdminException(AdminException::ROLE_ROLE_DELETE_FAULT);
        }

        $result = [
            'status'  => trans('admin/v1/controller.general.success_status'),
            'message' => trans('admin/v1/controller.role.success_role_delete'),
        ];

        return $this->success($result);
    }
}
